const request =require('./request')
const Router = {

    async albums( query ) {
        const { id, pageNo = 1, pageno = pageNo } = query;
        if (!id) {
            return {
                result: 500,
                errMsg: 'id ?'
            };
        }
        let html = await request(`http://music.migu.cn/v3/music/artist/${id}/album?page=${pageno}`, );

        const list = [];
        html = html.substring(html.indexOf("全部专辑"),html.indexOf("/全部专辑"));

        let listStr = html.substring(html.indexOf("<ul>"),html.indexOf("</ul>"))
        listStr = listStr.split("</li>");
        for (let str of listStr) {
            //找id
            let oneItemId = str.substring(str.indexOf("data-id=\"")+9);
            oneItemId = oneItemId.substring(0,oneItemId.indexOf("\""));
            //找img
            let oneItemImg = str.substring(str.indexOf("thumb-img lazy-image"));
            oneItemImg = oneItemImg.substring(oneItemImg.indexOf("data-original=\"")+15);
            oneItemImg = oneItemImg.substring(0,oneItemImg.indexOf("\""));
            //找name歌手
            let oneItemName = str.substring(str.indexOf("class=\"singer\""));
            oneItemName = oneItemName.substring(oneItemName.indexOf("\">")+2,oneItemName.indexOf("<"))
            //找专辑名
            let oneItemTitle = str.substring(str.indexOf("class=\"album-name\""));
            oneItemTitle = oneItemTitle.substring(oneItemTitle.indexOf("\">")+2,oneItemTitle.indexOf("<"))
            if(oneItemId.length>2) {
                list.push({
                    id: oneItemId,
                    picUrl: oneItemImg,
                    artists: oneItemName,
                    name: oneItemTitle
                })
            }
        }
        let pageStr = html.substring(html.indexOf("</style>"))
        let pageCount = 0;
        while (pageStr.indexOf("pagination-item") !== -1){
            pageCount++;
            pageStr = pageStr.substring(pageStr.indexOf("pagination-item") + 8);
        }
        return {
            result: 100,
            data: { list, totalPage: pageCount },
        };
    },
};
exports.default = Router;
